<? include 'db_info.php'; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>트러스 합주시간표</title>
<style type="text/css">
body { color: black; font-size: 10pt; font-family: 맑은고딕, 돋움;}
td { padding: 5px; }
a { color: black; text-decoration:none; }
a.emph { color: #FF4848; font-weight: bold; font-size: 14pt; }
</style>
<link rel="shortcut icon" href="images/sound.ico" />
<link rel="apple-touch-icon" href="images/sound.png" />
</head>
<body>
	<!-- outer table -->
	<table border="0" cellspacing="0" cellpadding="0" width="850" align="center">
		<!-- menu link -->
		<tr>
			<td colspan="2" align="center">
				<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
				<!-- timetable -->
				<ins class="adsbygoogle"
				style="display:inline-block;width:728px;height:90px"
				data-ad-client="ca-pub-5628522240073835"
				data-ad-slot="3230054508"></ins>
				<script>
				(adsbygoogle = window.adsbygoogle || []).push({});
				</script>
			</td>
		</tr>
		<tr>
			<td align="left"><p>
				<!-- class="emph" means that link uses a.emph style in the header -->
				<a class="emph" href="make.php">합주팀 생성</a> / 
				<a class="emph" href="how_to_use.php">사용방법</a></p>
			</td>
			<td align="right">
			<!-- using 'get' method, input text will be transferred to 'search.php' -->
			<form method="get" action="search.php">검색: 
				<!-- actual input box -->
				<input type="text" name="keyword" size="15" maxlength="20" />
				<input type="submit" value="찾기" />
			</form>
		</td></tr>

		<?
		if (mysqli_connect_errno($connect)) {
			echo "<tr><td colspan=\"2\">";
			echo "<span style=\"color:red; font-size:30pt;\">Failed to connect to MySQL: </span>".mysqli_connect_error();
			echo "</td></tr>";
		} 
		?>

		<tr><td colspan="2">

		<!-- inner contents -->
		<!-- actual timetable -->
		<table border="1" bordercolor="#aaaaaa" cellspacing="0" cellpadding="0" width="850" align="center">
			<tr align="center"><th colspan="8"><img src="images/title.jpg" border="0" alt="title picture"></th></tr>
			<tr align="center">
				<td width="80">시작 시간</td>
				<td width="110">월</td>
				<td width="110">화</td>
				<td width="110">수</td>
				<td width="110">목</td>
				<td width="110">금</td>
				<td width="110">토</td>
				<td width="110">일</td>
			</tr>

		<?
		// data row saving variable
		$row = array();

		// regular timetable
		$regular = mysqli_query($connect, "SELECT * FROM timetable");
		// temporary timetable
		$temporary = mysqli_query($connect, "SELECT * FROM timetable_temp");
		// $clock is for time computation
		// +=50 means to add half an hour
		for ($clock = 900; $clock < 2400; $clock += 50) {
			// time representation start
			$temp1 = $clock / 100; // quotient means hour
			$temp2 = $clock % 100; // modulo means whether o'clock or half an hour
			$ampm = "오전"; // initial 
			if ($clock >= 1300) { $hour = (int)$temp1 - 12; $ampm = "오후"; } // 12-hour basis
			else $hour = (int)$temp1; // integer casting in order to remove floating point

			if ($temp2 == 0) { $minute = '00'; }
			else $minute = '30';
			?>
			<tr align="center">
				<!-- ex) 오전 9:00 -->
				<td align="right"><?=$ampm?> <?=$hour?>:<?=$minute?></td>
			<? 
			// time representation end

			// get data from selected row
			$reg_row = mysqli_fetch_array($regular);
			$tmp_row = mysqli_fetch_array($temporary);
			// loop for each day in a time row
			for ($i = 1; $i <= 7; $i++) { // $i[0]: time column. $i[1] ~ $i[7]: weekday column
				if ($reg_row[$i] != "") { // $reg_row: clock time, $i: weekday
					echo "<td bgcolor=\"#CFE3FF\">"; // skyblue
					echo "<a href=\"search.php?action=detail&teamname=$reg_row[$i]\">"; // link to 'search.php'
					echo "$reg_row[$i]"; 
					echo "</a></td>";
				}
				elseif ($tmp_row[$i] != "") { // $tmp_row: clock time, $i: weekday
					echo "<td bgcolor=\"#F6CECE\">"; // pink
					echo "<a href=\"search.php?action=detail&teamname=$tmp_row[$i]\">"; // link to 'search.php'
					echo "$tmp_row[$i]"; 
					echo "</a></td>";
				}
				else {
					echo "<td></td>";
				}
			}
			echo "</tr>"; // end of a time row
			// time occupation print-out end
		}
		// discard database selection
		mysqli_free_result($regular);
		mysqli_free_result($temporary);


		// in order not to make timeout error, always disconnect db at the end
		mysqli_close($connect);
		?>

		</table>
		<!-- inner table end -->

		</td></tr>
	</table>
	<!-- outer table end -->
</body>
</html>